package cn.edu.mju.controller;

import cn.edu.mju.Utils.ResponseCode;
import cn.edu.mju.Utils.ResultData;
import cn.edu.mju.pojo.Book;
import cn.edu.mju.service.BookService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//@RestController
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/findAllBook")
    public ModelAndView findAllBook(){
        ModelAndView mv = new ModelAndView();
        List<Book> bookList = bookService.findAllBook();
        mv.addObject(bookList);
        mv.setViewName("listBook");
        return mv;
    }

    @RequestMapping("/queryBook/{id}")
    public String queryBook(@PathVariable String id){
       Book book = bookService.queryBook(Integer.parseInt(id));
        return JSONObject.toJSONString(book);
    }

    //跳转添加页面
    @RequestMapping(value = "/addBook",method = RequestMethod.GET)
    public ModelAndView addBook(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("addBook");
        return mv;
    }

    //接受post数据添加图书
    @RequestMapping(value = "/addBook",method = RequestMethod.POST)
    @ResponseBody
    public ResultData addBook(Book book){
        try {
            bookService.addBook(book);
        }catch(Exception e){
            e.printStackTrace();
            return ResultData.fail(ResponseCode.ERROR.val(),"添加失败");
        }
        return ResultData.fail(ResponseCode.SUCCESS.val(),"添加成功");
    }

    //更新图书
    @RequestMapping(value = "/editBook/{bookID}",method = RequestMethod.GET)
    public ModelAndView updateBook(@PathVariable int bookID){
        Book book = bookService.queryBook(bookID);
        ModelAndView mv = new ModelAndView();
        mv.addObject("book",book);
        mv.setViewName("editBook");
        return mv;
    }

    //更新图书
    @RequestMapping(value = "/editBook",method = RequestMethod.POST)
    @ResponseBody
    public ResultData updateBook(Book book){
        try {
            bookService.updateBook(book);
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.fail(ResponseCode.ERROR.val(),"修改失败");
        }
        return ResultData.fail(ResponseCode.SUCCESS.val(),"修改成功");
    }

    //删除图书
    @RequestMapping("/delBook/{bookID}")
    @ResponseBody
    public ResultData delBook(@PathVariable String bookID){
        try {
            bookService.delBook(Integer.parseInt(bookID));
        }catch (Exception e){
            return ResultData.fail(ResponseCode.ERROR.val(),"删除失败");
        }
        return ResultData.fail(ResponseCode.SUCCESS.val(),"删除成功");
    }

    //模糊查询图书
    @RequestMapping(value = "/searchBook")
    public ModelAndView searchBook(String bookName){
        ModelAndView mv = new ModelAndView();
        List<Book> bookList;
        //前端没有传值将返回所有图书列表
        if (bookName!=null){
            bookList = bookService.searchBook(bookName);
        }else {
            bookList = bookService.findAllBook();
        }
        mv.addObject(bookList);
        mv.setViewName("listBook");
        return mv;
    }

    @RequestMapping(value = "/admin")
    public ModelAndView admin(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin");
        return mv;
    }

}
